package com.example.app.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.app.GoodsInfo;
import com.example.app.R;
import com.example.app.adapter.home.ChannelAdapter;
import com.example.app.adapter.home.CouponListAdapter;
import com.example.app.adapter.home.NewGoodsListAdapter;
import com.example.app.api.Apiservice;
import com.example.app.interfaces.ResponseCallBack;
import com.example.app.viewmodel.ResponseData;
import com.example.app.viewmodel.home.Banner;
import com.example.app.viewmodel.home.Home;
import com.example.app.viewmodel.home.NewGoodsList;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

public class HomeFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private com.youth.banner.Banner home_banner;
    private RecyclerView home_channel;
    private RecyclerView home_couponList;
    private RecyclerView home_newgoodslist;

    public HomeFragment() {
    }

    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    public void initView(View view){
        home_banner = view.findViewById(R.id.home_banner);
        home_channel = view.findViewById(R.id.home_channel);
        home_couponList = view.findViewById(R.id.home_couponList);
        home_newgoodslist = view.findViewById(R.id.home_newgoodslist);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        //初始化
        initView(view);

        //获取数据
        Apiservice.GetHome(new ResponseCallBack<ResponseData<Home>>() {
            @Override
            public void Success(ResponseData<Home> responseData) {
                List<Banner> banners = responseData.getData().getBanner();
                //1轮播图
                home_banner.setAdapter(new BannerImageAdapter<Banner>(banners) {
                            @Override
                            public void onBindView(BannerImageHolder holder, Banner data, int position, int size) {
                                //图片加载自己实现
                                Glide.with(holder.itemView)
                                        .load(data.getUrl())
                                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                        .into(holder.imageView);
                            }
                        })
                        .addBannerLifecycleObserver(HomeFragment.this)//添加生命周期观察者
                        .setIndicator(new CircleIndicator(view.getContext()));

                //2通道
                home_channel.setLayoutManager(new GridLayoutManager(view.getContext(),5));
                home_channel.setAdapter(new ChannelAdapter(view.getContext(),responseData.getData().getChannel()));

                //3优惠券
                Log.e("TAG", "Success: " + responseData.getData().getCouponList().size());
                home_couponList.setLayoutManager(new LinearLayoutManager(view.getContext()));
                home_couponList.setAdapter(new CouponListAdapter(view.getContext(),responseData.getData().getCouponList()));

                //4新品
                home_newgoodslist.setLayoutManager(new GridLayoutManager(view.getContext(),2));
                home_newgoodslist.setAdapter(new NewGoodsListAdapter(
                        view.getContext(),
                        responseData.getData().getNewGoodsList(),
                        new NewGoodsListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(NewGoodsList item) {
                        Intent intent = new Intent(getActivity(), GoodsInfo.class);
                        intent.putExtra("id", item.getId()); // 传递商品id
                        startActivity(intent);
                    }
                }));
            }
        });
        return view;
    }
}